package com.xms.queryview.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xms.datacore.service.DeleterService;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.service.IDependencyChecker;
import com.xms.event.core.AuthorizationStateChangedEvent;
import com.xms.event.core.IEventPublisher;
import com.xms.event.core.ObjectDeletedEvent;
import com.xms.queryview.core.QueryViewDefaults;
import com.xms.queryview.entity.QueryView;
import com.xms.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * QueryviewDeleterService
 * </p>
 *
 * @author migo
 * @since 2021/9/3
 */
@CacheConfig(cacheNames = "queryview", keyGenerator = "CacheKeyGenerator")
@Service
public class QueryViewDeleterService extends DeleterService<QueryView> implements IQueryViewDeleterService {
    private IQueryViewDependency _queryViewDependency;
    private IDependencyChecker _dependencyChecker;
    private IEventPublisher _eventPublisher;

    @Autowired
    public QueryViewDeleterService(IQueryViewDependency queryViewDependency,
                                   IDependencyChecker dependencyChecker,
                                   IEventPublisher eventPublisher) {
        _queryViewDependency = queryViewDependency;
        _dependencyChecker = dependencyChecker;
        _eventPublisher = eventPublisher;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean delete(QueryView deleted) throws SQLException, XmsDependencyException {
        if (deleted == null) {
            return false;
        }
        //检查依赖项
        _dependencyChecker.checkAndThrow(QueryViewDefaults.MODULE_NAME, deleted.getQueryviewid());
        boolean success = super.deleteById(deleted.getQueryviewid());
        if (success) {
            //删除依赖项
            _queryViewDependency.delete(deleted.getQueryviewid());
            //权限更改事件
            _eventPublisher.publish(new AuthorizationStateChangedEvent(QueryViewDefaults.MODULE_NAME, Collections.singletonList(deleted.getQueryviewid()), false));
            //删除事件
            _eventPublisher.publish(new ObjectDeletedEvent<>(QueryViewDefaults.MODULE_NAME, deleted));
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteById(UUID id) throws SQLException, XmsDependencyException {
        QueryView entity = super.getById(id);
        if (entity == null)
            return false;
        return this.delete(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByIds(Collection<UUID> idList) throws XmsDependencyException, SQLException {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        List<QueryView> deleteds = super.listByIds(idList);
        if (CollectionUtil.notEmpty(deleteds)) {
            for (QueryView entity : deleteds) {
                this.delete(entity);
            }
            return true;
        }
        return false;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteMany(Wrapper<QueryView> queryWrapper) throws SQLException, XmsDependencyException {
        List<QueryView> deleteds = super.list(queryWrapper);
        if (CollectionUtil.notEmpty(deleteds)) {
            for (QueryView entity : deleteds) {
                this.delete(entity);
            }
            return true;
        }
        return false;
    }
}
